/**
 * 
 */
package cc.w3d.jawos.apps.artiks.client.components;

import cc.w3d.jawos.apps.artiks.client.Artiks;
import cc.w3d.jawos.apps.artiks.client.widgets.IconTreeItem;
import cc.w3d.jawos.apps.artiks.manager.client.beans.ResourceItem;
import cc.w3d.jawos.apps.artiks.manager.client.beans.ResourceType;

public class ResourceTreeItem extends IconTreeItem
{
	public ResourceTreeItem(ResourceItem item)
	{
		super
			(item != null && item.type == ResourceType.ARTICLE ? Artiks.icons.doc() : Artiks.icons.folder(),
			 item != null ? item.name : "/");

		super.setUserObject(item);

		if(item == null || item.type == ResourceType.FOLDER)
			addItem("");
	}

	@Override
	public ResourceItem getUserObject()
		{return (ResourceItem)super.getUserObject();}

	@Override
	public void setUserObject(Object userObj)
	{
		if(!(userObj instanceof ResourceItem))
			throw new RuntimeException("Tree item must be a ResourceItem");

		setUserObject((ResourceItem)userObj);
	}

	public void setUserObject(ResourceItem item)
	{
		setIcon(item != null && item.type == ResourceType.ARTICLE ? Artiks.icons.doc() : Artiks.icons.folder());
		setText(getName());

		super.setUserObject(item);
	}

	public String getId()
	{
		if(getUserObject() == null) return null;
		return getUserObject().id;
	}

	public String getName()
	{
		if(getUserObject() == null) return "/";
		return getUserObject().name;
	}

	public ResourceType getType()
	{
		if(getUserObject() == null) return ResourceType.FOLDER;
		return getUserObject().type;
	}

	public boolean isRoot(){return getUserObject() == null;}
	public boolean isFolder(){return isRoot() || getUserObject().type == ResourceType.FOLDER;}
}